﻿using System.Collections;
using System.Collections.Generic;
using Const;
using DG.Tweening;
using Entitas;
using Services;
using UnityEngine;


public class GameItemView : View,IComponentsItemIndexListener,IComponentsLoadSpriteListener
{
    public void OnComponentsItemIndex(GameEntity entity, customData index)
    {
        var DO=transform.DOMove(new Vector3(index.x, index.y), 0.3f);
        DO.OnComplete(() => _gameEntity.isComponentsMoveComplete=true);
        
       
    }

    public override void Link(IEntity entity, IContext context)
    {
        base.Link(entity, context);
        _gameEntity.AddComponentsDestroyListener(this);
        _gameEntity.AddComponentsItemIndexListener(this);
        _gameEntity.AddComponentsLoadSpriteListener(this);
        transform.position=new Vector3(_gameEntity.componentsItemIndex.index.x
            ,Contexts.sharedInstance.game.componentsGameBoardComponents.raws);

        IView _audioView = gameObject.AddComponent<AudioView>();
        _audioView.Link(entity,context);
    }

    public override void OnComponentsDestroy(GameEntity entity)
    {
        base.OnComponentsDestroy(entity);
        float time = 0.3f;
        transform.DOScale(Vector3.one * 1.5f, time);
        transform.GetComponent<SpriteRenderer>()
            .DOColor(Color.clear, time)
            .OnComplete(()=>Destroy(this.gameObject));
          
    }

    public void OnComponentsLoadSprite(GameEntity entity, string spriteName)
    {
        GetComponent<SpriteRenderer>()
            .sprite = Resources.Load<Sprite>(ResPath.SpritePath + spriteName);
        
        //Debug.Log("listener:"+ResPath.SpritePath + spriteName);
    }
}
